﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.IO;

namespace DictManage.Code
{
    public class OleDbAccess
    {
        static string file = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "\\ComponentData.mdb;User ID=Admin;Jet OLEDB:Database Password=;";
        private static OleDbConnection con;
        public static void Close()
        {
            if (con != null)
            {
                con.Close();
            }
            con = null;
        }
        private static OleDbCommand CreateCommand(string sqlStr)
        {
            Open();
            return new OleDbCommand { CommandType = CommandType.Text, CommandText = sqlStr, Connection = con };
        }
        public static bool ExecuteNonQury(string sqlStr)
        {
            OleDbCommand command = CreateCommand(sqlStr);
            int num = command.ExecuteNonQuery();
            if ((num == -1) | (num == 0))
            {
                command.Dispose();
                Close();
                return false;
            }
            command.Dispose();
            Close();
            return true;
        }
        public static DataSet GetDataSet(string sqlStr)
        {
            DataSet dataSet = new DataSet();
            OleDbCommand selectCommand = CreateCommand(sqlStr);
            OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand);
            adapter.Fill(dataSet);
            selectCommand.Dispose();
            Close();
            adapter.Dispose();
            return dataSet;
        }
        private static void Open()
        {
            if (con == null)
            {
                con = new OleDbConnection(file);
            }
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
        }
    }
}
